package com.edwin.websitebe.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.edwin.websitebe.constant.OSSConstant;
import com.edwin.websitebe.vo.Ueditor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

@Component
@Slf4j
public class OSSUtil {

    public static String baseUrl;

    public static String BASE_PATH = "/opt/website-resource/";
//    public static String BASE_PATH = "D:\\res\\";

    @Value("${oss.base-url}")
    public void setBaseUrl(String baseUrl) {
        OSSUtil.baseUrl = baseUrl;
    }

    public static Ueditor uploadEditorFile(MultipartFile file) {
        OSS ossClient = new OSSClientBuilder().build(OSSConstant.ENDPOINT, OSSConstant.ACCESS_KEY_ID, OSSConstant.ACCESS_KEY_SECRET);
        try {
            InputStream is = file.getInputStream();
            String nextId = IdUtil.getSnowflakeNextIdStr();
            String fileName = file.getOriginalFilename();
            String ext = fileName.substring(fileName.lastIndexOf("."));
            PutObjectRequest putObjectRequest = new PutObjectRequest(OSSConstant.BUCKET_NAME, nextId + ext, is);
            putObjectRequest.setProcess("true");
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            log.info("阿里云上传结果: {}", JSON.toJSONString(result));
            Ueditor ueditor = new Ueditor();
            ueditor.setState(Ueditor.ACTION_SUCCESS);
            ueditor.setUrl(result.getResponse().getUri());
            ueditor.setTitle(file.getName());
            ueditor.setOriginal(file.getOriginalFilename());
            return ueditor;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(ossClient)) {
                ossClient.shutdown();
            }
        }


    }

    public static String upload(String objName, MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        OSS client = new OSSClientBuilder().build(OSSConstant.ENDPOINT, OSSConstant.ACCESS_KEY_ID, OSSConstant.ACCESS_KEY_SECRET);
        PutObjectRequest putObjectRequest = new PutObjectRequest(OSSConstant.BUCKET_NAME, objName, is);
        putObjectRequest.setProcess("true");
        PutObjectResult result = client.putObject(putObjectRequest);
        return result.getResponse().getUri();
    }

    public static String uploadFile(String filePath, MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        BufferedOutputStream outputStream = FileUtil.getOutputStream(BASE_PATH + filePath);
        IoUtil.copy(inputStream, outputStream, IoUtil.DEFAULT_BUFFER_SIZE);
        return baseUrl + filePath;
    }

    public static String parseFilePath(String url) {
        return url.replace(baseUrl, BASE_PATH);
    }

}
